package com.javakc.cms.book.service;

import com.javakc.cms.book.dao.BookDao;
import com.javakc.cms.book.entity.Book;
import com.javakc.cms.book.vo.BookQuery;
import com.javakc.commonutils.jpa.base.service.BaseService;
import com.javakc.commonutils.jpa.dynamic.SimpleSpecificationBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BookService
 * @Description 书籍信息管理 - 逻辑层
 * @Author xujie
 * @Date 2021/11/3
 * @Version 1.0.0
 **/
@Service
public class BookService extends BaseService<BookDao,Book> {

    @Autowired
    private BookDao bookDao;
    @Autowired
    private EntityManager entityManager;

    public List<Book> findBook(){
        return dao.findAll();
    }

    /**
     * 分页查询
     * @param pageNo 当前页
     * @param pageSize 每页最大条数
     * @param bookQuery 查询封装对象
     * @return page
     */
    public Page<Book> findPage(int pageNo, int pageSize, BookQuery bookQuery){

        SimpleSpecificationBuilder simpleSpecificationBuilder = new SimpleSpecificationBuilder();
        if(StringUtils.hasLength(bookQuery.getName())){
            simpleSpecificationBuilder.and("name",":",bookQuery.getName());
        }
        if (StringUtils.hasLength(bookQuery.getBeginDate())) {
            simpleSpecificationBuilder.and("startTime", "ge", bookQuery.getBeginDate());
        }
        if (StringUtils.hasLength(bookQuery.getEndDate())) {
            simpleSpecificationBuilder.and("startTime", "lt", bookQuery.getEndDate());
        }
        if(!ObjectUtils.isEmpty(bookQuery.getId())) {
            simpleSpecificationBuilder.and("id", "=", bookQuery.getId());
        }
        if(!ObjectUtils.isEmpty(bookQuery.getOnline())) {
            simpleSpecificationBuilder.and("online", "=", bookQuery.getOnline());
        }
        if(!ObjectUtils.isEmpty(bookQuery.getOriginal())) {
            simpleSpecificationBuilder.and("original", "=", bookQuery.getOriginal());
        }
        if(!ObjectUtils.isEmpty(bookQuery.getCharge())) {
            simpleSpecificationBuilder.and("charge", "=", bookQuery.getCharge());
        }
        if(!ObjectUtils.isEmpty(bookQuery.getFirstSort())) {
            simpleSpecificationBuilder.and("firstSort", "=", bookQuery.getFirstSort());
        }
        if(!ObjectUtils.isEmpty(bookQuery.getSecondSort())) {
            simpleSpecificationBuilder.and("secondSort", "=", bookQuery.getSecondSort());
        }
        if(StringUtils.hasLength(bookQuery.getAuthor())){
            simpleSpecificationBuilder.and("name",":",bookQuery.getAuthor());
        }
        Page<Book> page = dao.findAll(simpleSpecificationBuilder.getSpecification(), PageRequest.of(pageNo - 1,pageSize));
        return page;
    }

    public List<Map<Integer,String>> queryFirstSort(){
        return dao.queryFirstSort();
    }

    public List<Map<Integer,String>> querySecondSort(Integer firstId){
        return dao.querySecondSort(firstId);
    }

    public String viewSecondSort(Integer secondId){
        return dao.viewSecondSort(secondId);
    }

}
